<template>
  <el-dialog  :visible.sync="visible" width="700px"  label-width="auto" :before-close="handleClose" >
      <div class="mg-bottom-10">
        <h2 style="text-align:center">招商项目</h2>
      </div>
      <div class="bottom_box">
      <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="200px">
          <el-form-item label="上传图片" ref="uploadElement"  >
           <UploadImg is-show-tip :limit="1" @success="handleUploadFileSuccess">
              <div class="upload-info">
                <img v-if="imageUrl" :src="imageUrl | imageFormat" class="avatar"/>
                <i
                  v-else
                  class="el-icon-plus avatar-uploader-icon"
                  style="width: 100px; height: 100px;"
                />
              </div>
            </UploadImg>
          </el-form-item>
          <el-form-item label="项目标题"  prop="name">
              <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="项目类型"  prop="typeId">
          <el-select v-model="ruleForm.typeId" placeholder="请选择">
            <el-option
              v-for="item in projectType"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="建筑面积"  prop="area">
              <el-input v-model="ruleForm.area"></el-input>
          </el-form-item>
          <el-form-item label="装修程度"  prop="decorationDegree">
            <el-select v-model="ruleForm.decorationDegree" placeholder="请选择">
              <el-option
                v-for="item in renovation"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
            <el-form-item label="配套设施"  prop="facilities">
            <el-input type="textarea"   v-model="ruleForm.facilities"></el-input>
          </el-form-item>
            <el-form-item label="项目简介"  prop="briefIntroduction">
            <el-input type="textarea"  v-model="ruleForm.briefIntroduction"></el-input>
          </el-form-item>
          <el-form-item>
            <div style="margin-left:50px;">
              <el-button type="primary" @click="onSubmit('ruleForm')">保存</el-button>
              <el-button  @click="handleClose">取消</el-button>
            </div>
          </el-form-item>
      </el-form>
      </div>
  </el-dialog>
</template>
<script>
import UploadImg from '@/components/upload-img/index'
  import { SelectProjectId,Updateproject,Addproject } from '../api'
  export default {
    name: 'InfoDetail',
    components: { UploadImg },
    props: {
      visible: { type: Boolean, required: true, default: true },
      delogId: { type: String},
      projectType: { type: Array},
    },
    data() {
      return {
        imageUrl:'',
        ruleForm:{},
        // 装修程度 
        renovation:[
          {value:'精装',label:'精装'},
          {value:'简装',label:'简装'},
          {value:'工装',label:'工装'},
          {value:'毛坯',label:'毛坯'},
          {value:'其他',label:'其他'}
        ],
        rules: {
          name: [
            { required: true, message: '请填写项目标题', trigger: 'blur' },
          ],
          typeId: [
            { required: true, message: '请选择项目类型', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '请填写面积(㎡)', trigger: 'blur' }
          ],
          decorationDegree:[
            {required: true, message: '请填写装修程度', trigger: 'blur'},
          ],
          facilities:[
            {required: true, message: '请填写配套设施', trigger: 'blur'},
          ],
          briefIntroduction:[
            {required: true, message: '请填写项目简介', trigger: 'blur'},
          ],
        }
      }
    },
    mounted(){
    },
    methods: {
    // 上传图片
    handleUploadFileSuccess(fileList) {
      this.ruleForm.imgId = fileList[fileList.length - 1].fileId
      this.imageUrl = this.ruleForm.imgId
    },
    // 保存表单
    onSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.delogId){
            Updateproject(this.ruleForm).then(res => {
              if(res === true) {
                this.handleClose()
                this.$message({
                  message: '保存成功！',
                  type: 'success'
                })
                this.$parent.getList()
                this.$emit('update:basicsShow', false)
              }
            })
          }else{
            delete this.ruleForm.id
            Addproject(this.ruleForm).then(res => {
                this.$message({
                  message: '保存成功！',
                  type: 'success'
                })
                this.handleClose()
                this.$parent.getList()
                this.$emit('update:basicsShow', false)
            })
          }
        } else {
          return false
        }
      });
    },
    parentHandleclick(id) {
        SelectProjectId({ProjectId:id}).then(res => {
          this.ruleForm = res
          this.imageUrl = res.imgId
      })
    },
    clearImg() {
       this.imageUrl = '' 
    },
    // 关闭弹框
    handleClose() {
        this.$emit('update:visible', false)
        this.$refs.ruleForm.resetFields()
    },
  }
  }
</script> 
<style lang="scss" scoped>
 .bottom_box{
      line-height: 30px;
      letter-spacing:0px;
      color: #666;
      min-height: 200px;
      max-height: 600px;
      overflow-y: auto;
      font-size: 16px;
      // padding-right: vw(120px);
      overflow-y: hidden;
  }
 .avatar-uploader .el-upload {
    border: 1px dashed #ccc;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: vw(100px);
    height: vw(100px);
    line-height: vw(100px);
    text-align: center;
  }
  .avatar {
    width: vw(100px);
    height: vw(100px);
    display: block;
  }
::v-deep .avatar-uploader{
    border: 1px solid #ccc;
    width: vw(100px);
    height:vw(100px)
}
::v-deep .el-form-item__content{
    padding-left:-100px
}
::v-deep .el-form-item__label{
    width: vw(125px);
}
::v-deep .el-dialog__body{
    padding-top: 0;
}
::v-deep .el-input {
    width: 200px;
}
</style>
